package com.qn.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qn.example.mapper.*;
import com.qn.example.pojo.*;
import com.qn.example.pojo.dto.DishDto;
import com.qn.example.service.OrderService;
import com.qn.example.utils.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/24 14:22
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private AddressBookMapper addressBookMapper;


    @Override
    public void add(Orders orders) {
        // 获得当前用户id, 查询当前用户的购物车数据
        Long userId = orders.getUserId();
        // 根据当前登录用户id, 查询用户数据
        User user = userMapper.findUser(userId);
        // 根据地址ID, 查询地址数据
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookMapper.findAddressBookById(addressBookId);
        //获取当前用户下单时的购物车信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.findShoppingCartByUserId(userId);

        //生成当前订单的id
        Long longId = UUIDUtils.getLongId();
        orders.setId(longId);

        // 组装订单明细数据, 批量保存订单明细
        List<OrderDetail> orderDetails = new ArrayList<>();

        //定义一个变量用来存储订单总价
        BigDecimal totalAmount = new BigDecimal("0");
        for (ShoppingCart shoppingCart : shoppingCarts) {
            //创建订单明细
            OrderDetail orderDetail = new OrderDetail();
            //给订单详情先关联订单（将订单id添加到订单详情中）
            orderDetail.setOrderId(orders.getId());
            //添加菜品或者套餐名称
            orderDetail.setName(shoppingCart.getName());
            //向订单详情中添加菜品Id
            orderDetail.setDishId(shoppingCart.getDishId());
            //向订单详情中添加套餐ID
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            //向订单详情中添加当前菜的口味信息
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            //每一个菜品的购买数量
            orderDetail.setNumber(shoppingCart.getNumber());
            //添加每一个菜的单价
            orderDetail.setAmount(shoppingCart.getAmount());
            //添加菜品的图片
            orderDetail.setImage(shoppingCart.getImage());
            //将每一个菜品对应的订单险情添加到菜品详情集合中
            orderDetails.add(orderDetail);

            //计算当前订单的总价
            totalAmount = totalAmount.add(shoppingCart.getAmount()
                    .multiply(new BigDecimal(shoppingCart.getNumber() + "")));
        }

        // 组装订单数据, 批量保存订单数据
        orders.setNumber(longId + "");//订单号
        orders.setStatus(1);//订单状态：1待付款，2待派送，3已派送，4已完成，5已取消
        orders.setOrderTime(LocalDateTime.now());//下单时间
        orders.setAmount(totalAmount);//订单总金额
        orders.setAddress(addressBook.getDetail());//订单的地址详情
        orders.setPhone(addressBook.getPhone());//收件人的电话
        orders.setConsignee(addressBook.getConsignee());//收件人姓名
        //将订单添加到数据库中
        orderMapper.save(orders);
        //将该订单对应的订单详情进行批量插入
        orderDetailMapper.batchSave(orderDetails);
        // 删除当前用户的购物车列表数据
        shoppingCartMapper.deleteByUserId(userId);

    }

    @Override
    public Page<Orders> findByPage(Integer page, Integer pageSize) {
        //1. 设置当前页与页面大小
        PageHelper.startPage(page, pageSize);

        //2.查询分页数据
        List<Orders> ordersList = orderMapper.findByPage(null);

        //3.封装PageInfo对象
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
        //4.封装返回页面的Page对象
        Page<Orders> objectPage = new Page<>();
        objectPage.setRecords(ordersPageInfo.getList());
        objectPage.setPage(ordersPageInfo.getPageNum());
        objectPage.setTotal(ordersPageInfo.getTotal());
        objectPage.setPageSize(ordersPageInfo.getPageSize());
        return objectPage;
    }
}
